package com.erdos.core.web;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.erdos.common.web.BaseAction;
import com.erdos.core.entity.ContentText;
import com.erdos.core.entity.FieldDefinition;
import com.erdos.core.entity.RefTable;
import com.erdos.core.entity.RefTableDetail;
import com.erdos.core.service.ContentTextService;
import com.erdos.core.service.FieldDefinitionService;
import com.erdos.core.service.RefTableDetailService;
import com.erdos.core.service.RefTableService;

public class RefTableDetailAction extends BaseAction<RefTableDetail> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4095178672068615179L;

	private static final Log logger = LogFactory.getLog(RefTableDetailAction.class);
	
	private RefTableDetailService refTableDetailService;
	private RefTableService refTableService ;
	private FieldDefinitionService fieldDefinitionService ;
	private ContentTextService contentTextService ;
	private String refTableSidForDetailShow ;
	private String refTableIdForDetailShow ;
	private RefTable tableEntity ;
	private Map<String,FieldDefinition> tableFieldMap = new HashMap<String,FieldDefinition>() ;
	private Map<String,String> contentMap = new LinkedHashMap<String,String>() ;
	
	private String refreshDivID ;
	
	
	public String detailFindByPage() throws Exception{
		
		Map<String,Object> params = new HashMap<String,Object>() ;
		params.put("refTableId",getQm().get("refTableId")) ;
		tableEntity = refTableService.findUniqueByParams(params) ;
		String result = SUCCESS ;
		if(tableEntity!=null){
			result = super.findByPage() ;
			getRelatedValue() ;
		}else{
			addActionError(getText("referenceTable.notExist")) ;
		}
		return result ;
	}
	
	public String detailInput() throws Exception{
		String res = super.input() ;
		Map<String,Object> params = new HashMap<String,Object>() ;
		params.put("refTableId",getModel().getRefTableId()) ;
		tableEntity = refTableService.findUniqueByParams(params) ;
		if(tableEntity!=null) {
			this.refTableIdForDetailShow = tableEntity.getRefTableId() ;
			this.refTableSidForDetailShow = tableEntity.getSid()+"" ;
			getRelatedValue() ;
		}else{
			addActionError(getText("referenceTable.notExist")) ;
		}
		
		return res ;
	}
	
	
	public String detailDelete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String sid = request.getParameter("sid");
		int deleteCount = 0 ;
		try{
			deleteCount = this.refTableDetailService.deleteByPrimaryKey(Long.parseLong(sid)) ;
		}catch(Exception e)
		{
			addActionError(getText("referenceTableDetail.delete.error")) ;
			return INPUT ;
		}
		request.setAttribute("method", "delete");
		saveBizMessage(getText("tips.delete.success",  new String[] { String.valueOf(deleteCount) }));
		return SUCCESS;
		
	}
	
	/**
	 * 获取与ReferenceTableDetail表显示时的相关信息
	 * 查询ReferenceTableDetail的数据时,获取对应的ReferenceTable中的数据表定义(tableEntity)后,
	 * 获取FieldDefinition表中对应的该表的数据结构,
	 * 再依据此FieldDefinition表中的数据取得Content表中最后要显示的标签名字
	 * @author LvXiaoguang
	 * @since  2008-7-28
	 */
	private void getRelatedValue() {
		//要显示的标签的语言
		String language = this.getLocale().toString() ;
		
		//判断ReferenceTable中定义的表结构中的列是否为空
		if(tableEntity.getKey1FieldSid()!=null){
			//获取Field实体
			FieldDefinition tempField = fieldDefinitionService.findByPrimaryKey(Long.parseLong(tableEntity.getKey1FieldSid()+"") );
			
			//存放入Map
			tableFieldMap.put("key1Value", tempField) ;
			if(tempField!=null)
			{
				Map<String,Object> params = new HashMap<String,Object>() ;
				params.put("contentId", tempField==null?"":tempField.getContentId()) ;
				params.put("language", language) ;
				
				//根据Field实体中的ContentID及系统Language取得对应的ContentText标签
				List<ContentText> tempList = contentTextService.findByParams(params) ;
				if(tempList!=null && tempList.size()>0){
					//存入contentMap
					contentMap.put("key1Value", tempList.get(0).getContentText()) ;
				}else{
					//如果取不到ContentText标签显示,则默认使用列标题显示
					contentMap.put("key1Value", "key1Value") ;
					logger.debug("detailFindByPage()-->getRelatedValue()-->取不到ContentId="+tempField.getContentId()+"对应的Content_text内容") ;
				}
			}else{
				//如果取不到对应的FieldDefinition对象,则默认使用列标题显示
				contentMap.put("key1Value", "key1Value") ;
				logger.debug("detailFindByPage()-->getRelatedValue()-->取不到FieldDefinition中Sid="+tableEntity.getKey1FieldSid()+"的记录") ;
			}
		}else{
			contentMap.put("key1Value", null) ;
		}
		
		//判断key2FieldSid是否为空
		if(tableEntity.getKey2FieldSid()!=null){
			//获取Field实体
			FieldDefinition tempField = fieldDefinitionService.findByPrimaryKey(Long.parseLong(tableEntity.getKey2FieldSid()+"") );
			
			//存放入Map
			tableFieldMap.put("key2Value", tempField) ;
			if (tempField!=null){
				Map<String,Object> params = new HashMap<String,Object>() ;
				params.put("contentId", tempField==null?"":tempField.getContentId()) ;
				params.put("language", language) ;
				
				//根据Field实体中的ContentID及系统Language取得对应的ContentText标签
				List<ContentText> tempList = contentTextService.findByParams(params) ;
				if(tempList!=null && tempList.size()>0){
					//存入contentMap
					contentMap.put("key2Value", tempList.get(0).getContentText()) ;
				}else{
					
					//如果取不到ContentText标签显示,则默认使用列标题显示
					contentMap.put("key2Value", "key2Value") ;
					logger.debug("detailFindByPage()-->getRelatedValue()-->取不到ContentId="+tempField.getContentId()+"对应的Content_text内容") ;
				}
			}else{
				
				//如果取不到对应的FieldDefinition对象,则默认使用列标题显示
				contentMap.put("key2Value", "key2Value") ;
				logger.debug("detailFindByPage()-->getRelatedValue()-->取不到FieldDefinition中Sid="+tableEntity.getKey1FieldSid()+"的记录") ;
			}
		}else{
			contentMap.put("key2Value", null) ;
		}
		
		//判断data1FieldSid是否为空
		if(tableEntity.getData1FieldSid()!=null){
			//获取Field实体
			FieldDefinition tempField = fieldDefinitionService.findByPrimaryKey(Long.parseLong(tableEntity.getData1FieldSid()+"") );
			tableFieldMap.put("data1Value", tempField) ;
			if(tempField!=null){
				Map<String,Object> params = new HashMap<String,Object>() ;
				params.put("contentId", tempField==null?"":tempField.getContentId()) ;
				params.put("language", language) ;
				
				//根据Field实体中的ContentID及系统Language取得对应的ContentText标签
				List<ContentText> tempList = contentTextService.findByParams(params) ;
				if(tempList!=null && tempList.size()>0){
					contentMap.put("data1Value", tempList.get(0).getContentText()) ;
				}else{
					contentMap.put("data1Value", "data1Value") ;
					logger.debug("detailFindByPage()-->getRelatedValue()-->取不到ContentId="+tempField.getContentId()+"  language="+language+"对应的Content_text内容") ;
				}
			}else{
				contentMap.put("data1Value", "data1Value") ;
				logger.debug("detailFindByPage()-->getRelatedValue()-->取不到FieldDefinition中Sid="+tableEntity.getKey1FieldSid()+"的记录") ;
			}
		}else{
			contentMap.put("data1Value", null) ;
		}
		
		//判断data2FieldSid是否为空
		if(tableEntity.getData2FieldSid()!=null){
			FieldDefinition tempField = fieldDefinitionService.findByPrimaryKey(Long.parseLong(tableEntity.getData2FieldSid()+"") );
			tableFieldMap.put("data2Value", tempField) ;
			if(tempField!=null){
				Map<String,Object> params = new HashMap<String,Object>() ;
				params.put("contentId", tempField==null?"":tempField.getContentId()) ;
				params.put("language", language) ;
				
				//根据Field实体中的ContentID及系统Language取得对应的ContentText标签
				List<ContentText> tempList = contentTextService.findByParams(params) ;
				if(tempList!=null && tempList.size()>0){
					contentMap.put("data2Value", tempList.get(0).getContentText()) ;
				}else{
					contentMap.put("data2Value", "data2Value") ;
					logger.debug("detailFindByPage()-->getRelatedValue()-->取不到ContentId="+tempField.getContentId()+"  language="+language+"对应的Content_text内容") ;
				}
			}else{
				contentMap.put("data2Value", "data2Value") ;
				logger.debug("detailFindByPage()-->getRelatedValue()-->取不到FieldDefinition中Sid="+tableEntity.getKey1FieldSid()+"的记录") ;
			}
		}else{
			contentMap.put("data2Value", null) ;
		}
		
		//判断data3FieldSid是否为空
		if(tableEntity.getData3FieldSid()!=null){
			FieldDefinition tempField = fieldDefinitionService.findByPrimaryKey(Long.parseLong(tableEntity.getData3FieldSid()+"") );
			tableFieldMap.put("data3Value", tempField) ;
			if(tempField!=null){
				Map<String,Object> params = new HashMap<String,Object>() ;
				params.put("contentId", tempField==null?"":tempField.getContentId()) ;
				params.put("language", language) ;
				
				//根据Field实体中的ContentID及系统Language取得对应的ContentText标签
				List<ContentText> tempList = contentTextService.findByParams(params) ;
				if(tempList!=null && tempList.size()>0){
					contentMap.put("data3Value", tempList.get(0).getContentText()) ;
				}else{
					contentMap.put("data3Value", "data3Value") ;
					logger.debug("detailFindByPage()-->getRelatedValue()-->取不到ContentId="+tempField.getContentId()+"  language="+language+"对应的Content_text内容") ;
				}
			}else{
				contentMap.put("data3Value", "data3Value") ;
				logger.debug("detailFindByPage()-->getRelatedValue()-->取不到FieldDefinition中Sid="+tableEntity.getKey1FieldSid()+"的记录") ;
			}
		}else{
			contentMap.put("data3Value", null) ;
		}
		
		//判断data4FieldSid是否为空
		if(tableEntity.getData4FieldSid()!=null){
			FieldDefinition tempField = fieldDefinitionService.findByPrimaryKey(Long.parseLong(tableEntity.getData4FieldSid()+"") );
			tableFieldMap.put("data4Value", tempField) ;
			if(tempField!=null){
				Map<String,Object> params = new HashMap<String,Object>() ;
				params.put("contentId", tempField==null?"":tempField.getContentId()) ;
				params.put("language", language) ;
				
				//根据Field实体中的ContentID及系统Language取得对应的ContentText标签
				List<ContentText> tempList = contentTextService.findByParams(params) ;
				if(tempList!=null && tempList.size()>0){
					contentMap.put("data4Value", tempList.get(0).getContentText()) ;
				}else{
					contentMap.put("data4Value", "data4Value") ;
					logger.debug("detailFindByPage()-->getRelatedValue()-->取不到ContentId="+tempField.getContentId()+"  language="+language+"对应的Content_text内容") ;
				}
			}else{
				contentMap.put("data4Value", "data4Value") ;
				logger.debug("detailFindByPage()-->getRelatedValue()-->取不到FieldDefinition中Sid="+tableEntity.getKey1FieldSid()+"的记录") ;
			}
		}else{
			contentMap.put("data4Value", null) ;
		}
		
		//判断data5FieldSid是否为空
		if(tableEntity.getData5FieldSid()!=null){
			FieldDefinition tempField = fieldDefinitionService.findByPrimaryKey(Long.parseLong(tableEntity.getData5FieldSid()+"") );
			tableFieldMap.put("data5Value", tempField) ;
			if(tempField!=null){
				Map<String,Object> params = new HashMap<String,Object>() ;
				params.put("contentId", tempField==null?"":tempField.getContentId()) ;
				params.put("language", language) ;
				
				//根据Field实体中的ContentID及系统Language取得对应的ContentText标签
				List<ContentText> tempList = contentTextService.findByParams(params) ;
				if(tempList!=null && tempList.size()>0){
					contentMap.put("data5Value", tempList.get(0).getContentText()) ;
				}else{
					contentMap.put("data5Value", "data5Value") ;
					logger.debug("detailFindByPage()-->getRelatedValue()-->取不到ContentId="+tempField.getContentId()+"  language="+language+"对应的Content_text内容") ;
				}
			}else{
				contentMap.put("data5Value", "data5Value") ;
				logger.debug("detailFindByPage()-->getRelatedValue()-->取不到FieldDefinition中Sid="+tableEntity.getKey1FieldSid()+"的记录") ;
			}
		}else{
			contentMap.put("data5Value", null) ;
		}
	}
	
	public RefTableDetailService getRefTableDetailService() {
		return refTableDetailService;
	}
	public void setRefTableDetailService(RefTableDetailService refTableDetailService) {
		this.refTableDetailService = refTableDetailService;
	}
	public RefTableService getRefTableService() {
		return refTableService;
	}
	public void setRefTableService(RefTableService refTableService) {
		this.refTableService = refTableService;
	}
	public FieldDefinitionService getFieldDefinitionService() {
		return fieldDefinitionService;
	}
	public void setFieldDefinitionService(
			FieldDefinitionService fieldDefinitionService) {
		this.fieldDefinitionService = fieldDefinitionService;
	}
	public ContentTextService getContentTextService() {
		return contentTextService;
	}
	public void setContentTextService(ContentTextService contentTextService) {
		this.contentTextService = contentTextService;
	}

	public RefTable getTableEntity() {
		return tableEntity;
	}

	public void setTableEntity(RefTable tableEntity) {
		this.tableEntity = tableEntity;
	}

	public Map<String, FieldDefinition> getTableFieldMap() {
		return tableFieldMap;
	}

	public void setTableFieldMap(Map<String, FieldDefinition> tableFieldMap) {
		this.tableFieldMap = tableFieldMap;
	}

	public Map<String, String> getContentMap() {
		return contentMap;
	}

	public void setContentMap(Map<String, String> contentMap) {
		this.contentMap = contentMap;
	}

	public String getRefTableSidForDetailShow() {
		return refTableSidForDetailShow;
	}

	public void setRefTableSidForDetailShow(String refTableSidForDetailShow) {
		this.refTableSidForDetailShow = refTableSidForDetailShow;
	}

	public String getRefTableIdForDetailShow() {
		return refTableIdForDetailShow;
	}

	public void setRefTableIdForDetailShow(String refTableIdForDetailShow) {
		this.refTableIdForDetailShow = refTableIdForDetailShow;
	}

	public String getRefreshDivID() {
		return refreshDivID;
	}

	public void setRefreshDivID(String refreshDivID) {
		this.refreshDivID = refreshDivID;
	}
}
